package com.lucweb.dao.impl;

import com.lucweb.dao.QuestionDao;
import com.lucweb.entity.Question;
import com.lucweb.entity.TaskQuestion;
import org.hibernate.SessionFactory;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Transactional(rollbackFor = Exception.class)
@Repository("questionDao")
public class QuestionDaoImpl implements QuestionDao {

    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;


    @Override
    public List<TaskQuestion> getAllQuestion(String para) {
        //此处要修改单位名称
        List<TaskQuestion> questionList = (List<TaskQuestion>) sessionFactory.getCurrentSession().createQuery("FROM " +
                "TaskQuestion where "+para).list();

        return questionList;
    }


    @Override
    public void addQuestion(TaskQuestion taskQuestion) {
        sessionFactory.getCurrentSession().saveOrUpdate(taskQuestion);
    }

    @Override
    public TaskQuestion getQuestionById(int questionId) {
        return (TaskQuestion)sessionFactory.getCurrentSession().get(TaskQuestion.class, questionId);
    }

    @Override
    public void deleteQuestion(int questionId) {
        sessionFactory.getCurrentSession().delete(
                sessionFactory.getCurrentSession().get(TaskQuestion.class, questionId)
        );
    }

    @Override
    public int getMaxId() {

        Integer number = (Integer) sessionFactory.getCurrentSession().createSQLQuery("select auto_increment from " +
                "information_schema.tables where table_schema='firesystem' and table_name='task_question';")
                .addScalar("auto_increment", StandardBasicTypes.INTEGER).uniqueResult();
        return number;
    }
}
